import subprocess
import os

class F():
    def adduser(self,name):
        subprocess.call('sudo useradd %s' %name,shell = True)
class A(F):
    def catuser(self,name):
        subprocess.call('sudo id %s'%name,shell = True)
    def chuser(self):
        print("#"*20)
        print("1.修改用户uid")
        print("2.修改用户gid")
        print("3.修改用户shell")
        print("4.exit")
        print("#"*20)
        while True:
            name = input("请输入要修改的用户:")
            a = input("请输入你的选择:")
            if a=="1":
                uid = input("请输入uid:")
                subprocess.call(f'sudo usermod -u {uid} {name}',shell = True)
            elif a=="2":
                gid = input("请输入gid:")
                subprocess.call(f"sudo usermod -g {gid} {name}",shell = True)
            elif a=="4":
                return
            elif a=="3":
                sh = input("请输入用户shell:")
                subprocess.call(f"sudo usermod -s {sh} {name}",shell=True)
            else :
                print("请输入1-4")

class B(F):
    def deluser(self,name):
        subprocess.call(f"sudo userdel -r {name}",shell = True)
    def delgrp(self,gname):
        subprocess.call(f"sudo groupdel {gname}",shell = True)
class Except(Exception):
    def __str__(self):
        return "输入的username或gid不存在!"

class C(A,B):
    def addgrp(self,gid,gname):
        try:
            if subprocess.call(f"sudo groupadd -g {gid} {gname}",shell=True)==1:
                raise Except()
        except Except as e:
            print(e)
    def chgrp(self,name):
        gid=os.popen("awk -F ':' 'END {print$3}' /etc/group").readlines()
        gid1 = "".join(gid).strip()
        subprocess.call(f'sudo usermod -g {gid1} {name}',shell = True)
